package cn.hxzy.zyicon.controller;

import cn.hxzy.zyicon.entity.Resource;
import cn.hxzy.zyicon.entity.Role;
import cn.hxzy.zyicon.entity.User;
import cn.hxzy.zyicon.entity.po.RolePO;
import cn.hxzy.zyicon.entity.po.UserPO;
import cn.hxzy.zyicon.entity.vo.ReturnBean;
import cn.hxzy.zyicon.service.RoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @PostMapping("list")
    public ReturnBean list() {
        List<Role> all = roleService.findAll();
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS, all);
    }

    @PostMapping("add")
    public ReturnBean add(Role role) {
        roleService.save(role);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS);
    }
    @PostMapping("deleteId")
    public ReturnBean deleteId(Integer id) {
        roleService.delete(id);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS);
    }
    @PostMapping("findById")
    public ReturnBean findById(Integer id) {
        Role byId = roleService.findById(id);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS,byId);
    }
    @PostMapping("update")
    public ReturnBean update(RolePO rolePO) {
        Role role=new Role();
        BeanUtils.copyProperties(rolePO,role);
        roleService.update(role);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS);
    }
    @PostMapping("authorize")
    public ReturnBean authorize(Integer resourceId,Integer roleId) {
        roleService.authorize(resourceId, roleId);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS);
    }
    @PostMapping("findRoleNotHave")
    public ReturnBean findRoleNotHave(Integer id) {
        List<Resource> notHave = roleService.findNotHave(id);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS,notHave);
    }
    @PostMapping("findRoleById")
    public ReturnBean findRoleById(Integer id) {
        Role all = roleService.findById(id);
        if(all!=null){
            return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS, all);
        }
        return ReturnBean.of(ReturnBean.AnswerCode.OBJECT_NOT_EXIST);
    }

}
